package com.vast.system.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Random;

@Component
@Slf4j
public class SmsUtils {

    @Value("${sms.signName}")
    private String signName;

    @Value("${sms.accessKeyID}")
    private String accessKeyID;

    @Value("${sms.accessKeySecret}")
    private String accessKeySecret;

    @Value("${sms.regionId}")
    private String regionId;

    /**
     * 短信验证码
     * SMS_187260477
     */
    /**
     * 单条短信发送
     * @param phone 支持对多个手机号码发送短信，手机号码之间以英文逗号（,）分隔。上限为1000个手机号码。批量调用相对于单条调用及时性稍有延迟。
     * @param msg
     */
    public void sendSms(String phone, String templateCode, String msg) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", msg);
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info(response.getData());
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }

//    /**
//     * 批量发送短信
//     */
//    public void sendBatchSms() {
//        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
//        IAcsClient client = new DefaultAcsClient(profile);
//
//        CommonRequest request = new CommonRequest();
//        request.setMethod(MethodType.POST);
//        request.setDomain("dysmsapi.aliyuncs.com");
//        request.setVersion("2017-05-25");
//        request.setAction("SendBatchSms");
//        request.putQueryParameter("RegionId", regionId);
//        request.putQueryParameter("PhoneNumberJson", "1");
//        request.putQueryParameter("SignNameJson", "2");
//        request.putQueryParameter("TemplateCode", "3");
//        request.putQueryParameter("TemplateParamJson", "4");
//        try {
//            CommonResponse response = client.getCommonResponse(request);
////            System.out.println(response.getData());
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 发送短信验证码
     * @param phone
     * @param code
     */
    public void sendSmsValidateCode(String phone, String code) {
        String msg = "{\"code\":\"" + code + "\"}";
        sendSms(phone, "SMS_187260477", msg);
    }
    /**
     * 生成短信验证码
     * @return
     */
    public static String generateValidateCode() {
        Random random = new Random();
        return String.format("%06d", random.nextInt(1000000));
    }
}
